package mobileFromUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MobileMarkInfo {
	private String Mobile7;
	private String Province;
	private String City;
	private String MobileType;
	private Date DateCreated;

	public MobileMarkInfo() {
	}

	/**
	 * 手机号地区级卡类型
	 * 
	 * @param mobile7
	 *            手机号前七位
	 * @param province
	 *            省份
	 * @param city
	 *            城市
	 * @param mobileType
	 *            卡类型
	 * @param dateCreated
	 *            记录创建日期
	 */
	public MobileMarkInfo(String mobile7, String province, String city,
			String mobileType, Date dateCreated) {
		Mobile7 = mobile7;
		Province = province;
		City = city;
		MobileType = mobileType;
		DateCreated = dateCreated;
	}

	public String getMobile7() {
		return Mobile7;
	}

	public void setMobile7(String mobile7) {
		Mobile7 = mobile7;
	}

	public String getProvince() {
		return Province;
	}

	public void setProvince(String province) {
		Province = province;
	}

	public String getCity() {
		return City;
	}

	public void setCity(String city) {
		City = city;
	}

	public String getMobileType() {
		return MobileType;
	}

	public void setMobileType(String mobileType) {
		MobileType = mobileType;
	}

	public Date getDateCreated() {
		return DateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		DateCreated = dateCreated;
	}
	
	public static void main(String[] args) {
		MobileMarkInfo mobileMarkInfo = GetMobileMarkFromIP138("1875076");
		System.out.println(mobileMarkInfo.getCity());
	}

	public static MobileMarkInfo GetMobileMarkFromIP138(String mobile7) {
		// System.out.println(mobile7);
		// 请求URL
		String REQUEST_URL = "http://www.ip138.com:8080/search.asp";
		// 请求方法
		String REQUEST_MOTHOD = "POST";
		// 提取手机号码归属地的正则表达式
		String REGEX_EXP = "^<TD width=\\* align=\"center\" class=tdc2>(.*)</TD>$";
		// 编译后的正则表达式Pattern对象
		Pattern p = Pattern.compile(REGEX_EXP);

		MobileMarkInfo mobileMarkInfo = new MobileMarkInfo();

		BufferedReader br = null;

		try {
			HttpURLConnection httpConn = (HttpURLConnection) new URL(
					REQUEST_URL).openConnection();

			httpConn.setRequestMethod(REQUEST_MOTHOD);

			httpConn.setDoOutput(true);

			String requestParameter = "mobile=" + mobile7 + "&action=mobile";

			httpConn.getOutputStream().write(requestParameter.getBytes());

			httpConn.getOutputStream().flush();

			httpConn.getOutputStream().close();

			br = new BufferedReader(new InputStreamReader(httpConn
					.getInputStream(), "GBK"));

			String lineStr = null;

			while ((lineStr = br.readLine()) != null) {
				System.out.println(lineStr);
				if ("<TD width=\"130\" align=\"center\" noswap>卡号归属地</TD>"
						.equals(lineStr.trim())) {
					lineStr = br.readLine().trim();

					Matcher m = p.matcher(lineStr);
					if (m.matches()) {
						String mobileArea = m.group(1);
						// System.out.println(mobileArea);
						String[] areas = mobileArea.split("&nbsp;", 2);
						if (areas.length > 1) {
							mobileMarkInfo.setProvince(areas[0]);
							mobileMarkInfo.setCity(areas[1]);
						} else {
							return null;
						}
					}
				}
				if ("<TD width=\"130\" align=\"center\" noswap>卡&nbsp;类&nbsp;型</TD>"
						.equals(lineStr.trim())) {
					lineStr = br.readLine().trim();
					Matcher m = p.matcher(lineStr);
					if (m.matches()) {
						String cardType = m.group(1);
						// System.out.println(cardType);
						mobileMarkInfo.setMobileType(cardType);
						break;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			// Log.writeLog("MobileUtil.GetMobileMarkFromIP138 " +
			// e.getMessage());
		} finally {
			if (br != null)
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
					// Log.writeLog("MobileUtil.GetMobileMarkFromIP138 " +
					// e.getMessage());
				}
		}
		return mobileMarkInfo;
	}
}
